package com.car.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import com.car.bean.PageObj;
import com.car.util.Page;

public class PageUtil {
	
	/**
	 * 产生页面对象
	 * @param size
	 * @param curr
	 * @param listSize
	 * @return
	 */
	public static Page createPage(Integer size,Integer curr,Integer listSize) {
		Page page=new Page();
		Integer pageSize=(size==null || size<=0) ? 10:size;
		Integer currPage=curr==null?1:curr;
		//如果页面未设置则默认分页器的每页条目数，默认为15条
		if(pageSize==null || pageSize<=0){
			pageSize=(int) page.getPagesize();
		}else{
			//分页器设置每页条目数
			page.setPagesize(pageSize);
		}
		// 分页器设置当前页
		page.setCurrpage(currPage);
		if (null != listSize)
			page.setTotal(listSize);
		else {
			page.setTotal(0l);
		}
		

		
		return page;
	}
	
	
	
	/**
	 * 从request 获取参数
	 * @param request
	 * @return
	 */
	public static String parameterUri(HttpServletRequest request){
		Enumeration<String> enu = request.getParameterNames();
		StringBuffer url = new StringBuffer("");
		while(enu.hasMoreElements()){
			String paraName=(String)enu.nextElement();
			String value = request.getParameter(paraName);
			try {
				value=URLEncoder.encode(value, "UTF-8");
			} catch (UnsupportedEncodingException e) {
			}
			//System.out.println(paraName+": "+request.getParameter(paraName));
			if ("".equals(url.toString())){
				if (value!=null && !"".equals(value) && !"currPage".equals(paraName)){
					url.append(paraName+"="+String.valueOf((null==value?"":value)));
				}
			} else{
				if (value!=null && !"".equals(value) && !"currPage".equals(paraName)){
					url.append("&"+paraName+"="+String.valueOf((null==value?"":value)));
				}
			}
		}
		//System.out.println("url----"+request.getRequestURI()+"?"+url.toString());
		return request.getRequestURI()+"?"+url.toString();
	}
	
	/**
	 * 从request 获取参数
	 * @param request
	 * @return
	 */
	public static String parameterUri(HttpServletRequest request ,String ignorePara){
		Enumeration<String> enu = request.getParameterNames();
		StringBuffer url = new StringBuffer("");
		while(enu.hasMoreElements()){
			String paraName=(String)enu.nextElement();
			String value = request.getParameter(paraName);
			try {
				value=URLEncoder.encode(value, "UTF-8");
			} catch (UnsupportedEncodingException e) {
			}
			//System.out.println(paraName+": "+request.getParameter(paraName));
			/*
			if ("".equals(url.toString())){
				if (value!=null && !"".equals(value) && !ignorePara.equals(paraName)){
					url.append(paraName+"="+String.valueOf((null==value?"":value)));
				}
			} else{
				if (value!=null && !"".equals(value) && !ignorePara.equals(paraName)){
					url.append("&"+paraName+"="+String.valueOf((null==value?"":value)));
				}
			}*/
			if (value!=null && !"".equals(value) && !ignorePara.equalsIgnoreCase(paraName)){
				url.append("&"+paraName+"="+String.valueOf((null==value?"":value)));
			}
		}
		String ret_url = url.toString();
		try {
			if(ret_url.indexOf("&") == 0){
				ret_url  = ret_url.substring(1, ret_url.length());
			}
		} catch (Exception e) {
		}
		//System.out.println("url----"+request.getRequestURI()+"?"+url.toString());
		return request.getRequestURI()+"?"+url.toString();
	}
	
	/**
	 * YGY 新增方法。
	 * 从request 获取参数,并排除页码 和 特定的参数
	 * @param request
	 * @return
	 */
	public static String parameterUri(HttpServletRequest request ,String ignorePara,String currPage){
		Enumeration<String> enu = request.getParameterNames();
		StringBuffer url = new StringBuffer("");
		while(enu.hasMoreElements()){
			String paraName=(String)enu.nextElement();
			String value = request.getParameter(paraName);
			try {
				value=URLEncoder.encode(value, "UTF-8");
			} catch (UnsupportedEncodingException e) {
			}
			if(paraName.equalsIgnoreCase(currPage)){
				continue;
			}
			//System.out.println(paraName+": "+request.getParameter(paraName));
			if (value!=null && !"".equals(value) && !ignorePara.equalsIgnoreCase(paraName)){
				url.append("&"+paraName+"="+String.valueOf((null==value?"":value)));
			}
		}
		String ret_url = url.toString();
		try {
			if(ret_url.indexOf("&") == 0){
				ret_url  = ret_url.substring(1, ret_url.length());
			}
		} catch (Exception e) {
		}
		//System.out.println("url----"+request.getRequestURI()+"?"+url.toString());
		return request.getRequestURI()+"?"+ret_url;
	}
	public static PageObj createPages(Integer pagesize,Integer currpage) {
//		Integer pagesize=Integer.parseInt(vo.getPageSize());
//		Integer currpage=vo.getCurrPage();
		PageObj page = new PageObj();
		if (pagesize == null || pagesize <= 0) {
			pagesize = 10;
		}
		
		if (currpage != null) {
			int startP = (currpage - 1) * pagesize;
			page.setStart("" + startP);
		}
		page.setLimit("" + pagesize);
		return page;
	}

}
